package com.frxuexi.park2210.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.entity.ParkBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.park2210.R
import com.frxuexi.park2210.adapter.ParkAdapter
import com.frxuexi.park2210.databinding.ActivityParkBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel> (){
    val parkAdapter by lazy { ParkAdapter() }
    override fun initData() {
        vm.parkSuc.observe(this){
            val parkBean = Gson().fromJson(it, ParkBean::class.java).filter {
                !it.parkStart.isNullOrEmpty() && !it.parkName.isNullOrEmpty()
            }.sortedByDescending {
                it.parkId
            }
            Log.i("===",parkBean.toString())
            parkAdapter.addData(parkBean)
            parkAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.headerPark.apply {
            headerTitle.setText("车位管理")
            headerBack.setOnClickListener {
                finish()
            }
            headerAdd.visibility=View.VISIBLE
            headerAdd.setOnClickListener {
                ARouter.getInstance().build("/app/ParkAddActivity").navigation()
            }
        }

        binding.rvPark.apply {
            adapter=parkAdapter
            layoutManager=GridLayoutManager(this@ParkActivity,2)
        }

        parkAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/ParkVRActivity").navigation()
        }

        parkAdapter.addChildClickViewIds(R.id.park_update)
        parkAdapter.setOnItemChildClickListener { adapter, view, position ->
            if(view.id==R.id.park_update){
                parkAdapter.data.get(position).parkName="津A888888"
                parkAdapter.notifyItemChanged(position)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        parkAdapter.data.clear()
        vm.getParks()
    }

}